﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using RLMP = RelationsLMP;

namespace AlarmManagePlatform.Forms
{
    public partial class ImageBrowser : MetroFramework.Forms.MetroForm
    {
        public event Action<Model.CheckDataInfo> Event_UpdateImageResult;
        public ImageBrowser()
        {
            InitializeComponent();
            this.Text = "图片信息";
            flBitImageInfo.AutoScroll = true;
        }

        Model.CheckDataInfo checkDataInfo;
        public Model.CheckDataInfo CheckDataInfo
        {
            set { checkDataInfo = value;
                TestValue = float.Parse(checkDataInfo.ParaValue);
            }
        }

        public bool ShowTestValue
        {
            set {
                pTestValue.Visible = value;
            }
        }

        float testValue;
        
        public float TestValue
        {
            set {
                testValue = value;
                tbtestValue.Value = testValue;
            }
        }

        RLMP.Model.ImageRecognitionReslut[] bitMapFile;


        public RLMP.Model.ImageRecognitionReslut[] BitMapFile
        {
            set
            {
                if (value == null || value.Length <= 0) return;
                bitMapFile = value;
                Stream imageFileStream;
                try
                {
                    for (int i = 0; i < bitMapFile.Length; i++)
                    {
                        if (!string.IsNullOrWhiteSpace(bitMapFile[i].fileName) && File.Exists(Static.ParaInfo.ImageCapturePath + bitMapFile[i].fileName))
                        {
                            imageFileStream = File.Open(Static.ParaInfo.ImageCapturePath + bitMapFile[i].fileName, FileMode.Open);
                            bitMapFile[i].bitmap = (Bitmap)Image.FromStream(imageFileStream);
                            imageFileStream.Close();
                            imageFileStream.Dispose();
                        }
                    }
                    ShowBitMapInfo();
                }
                catch (Exception ex)
                {
                    throw new Exception("读取图片失败，错误信息：" + ex.Message);
                }
            }
        }

        void ShowBitMapInfo()
        {
            this.flBitImageInfo.Controls.Clear();
            if (bitMapFile == null || bitMapFile.Length <= 0) return;
            RLMP.Control.UC.ImageBrowserItem _ImageBrowserItem;
            for (int i = 0; i < bitMapFile.Length; i++)
            {
                _ImageBrowserItem = new RLMP.Control.UC.ImageBrowserItem();
                _ImageBrowserItem.Title = string.IsNullOrWhiteSpace(bitMapFile[i].fileName) ? "图片" + (i + 1) : bitMapFile[i].fileName;
                _ImageBrowserItem.Image = bitMapFile[i].bitmap;
                this.flBitImageInfo.Controls.Add(_ImageBrowserItem);
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            this.Visible = false;
            e.Cancel = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.testValue != tbtestValue.Value)
                {
                    float testValue = (float)tbtestValue.Value;
                    Bitmap bitmapTemp, bitmap;
                    string imageFilePath = string.Empty;
                    Graphics graphics;
                    Stream imageStream = null;
                    bool flag;
                    RLMP.Model.ImageRecognitionReslut imageRecognitionReslut = bitMapFile[bitMapFile.Length - 1];
                    if (!string.IsNullOrWhiteSpace(imageRecognitionReslut.fileName))
                        flag = true;
                    else
                        flag = false;

                    if (flag)
                    {
                        imageFilePath = Static.ParaInfo.ImageCapturePath + bitMapFile[bitMapFile.Length - 1].fileName;
                        if (!File.Exists(imageFilePath))
                        {
                            MessageBox.Show(this, string.Format("图片【{0}】丢失，无法修改。", imageFilePath), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                        imageStream = File.Open(imageFilePath, FileMode.Open);
                        bitmapTemp = (Bitmap)Image.FromStream(imageStream);
                    }
                    else
                        bitmapTemp = imageRecognitionReslut.bitmap;
                    bitmap = new Bitmap(bitmapTemp.Width, bitmapTemp.Height - 20);
                    graphics = Graphics.FromImage(bitmap);
                    graphics.Clear(Color.White);
                    graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.DrawImage(bitmapTemp, 0, 0, new Rectangle(0, 20, bitmapTemp.Width, bitmapTemp.Height - 20), GraphicsUnit.Pixel);
                    bitmapTemp.Dispose();
                    bitmapTemp = null;
                    if (flag)
                    {
                        imageStream.Close();
                        imageStream.Dispose();
                        if (File.Exists(imageFilePath))
                            File.Delete(imageFilePath);
                        bitmap.Save(imageFilePath);
                        //Common.Methods.SaveIdentificationResult(bitmap, imageFilePath, testValue.ToString(), false);
                    }
                    graphics.Dispose();
                    graphics = null;
                    BitMapFile = bitMapFile;
                    checkDataInfo.ParaValue = testValue.ToString();
                    Event_UpdateImageResult?.Invoke(checkDataInfo);
                }                
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this,"更新失败，错误信息：" + ex.Message,"异常信息",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }
}
